package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.Req.BatchContentReq;
import com.example.Req.KeywordReq;
import com.example.common.Pagination;
import com.example.mapper.IndicatorBatchMapper;
import com.example.model.FcapNoContent;
import com.example.service.FcapNoContentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Slf4j
public class FcapNoContentServiceImpl implements FcapNoContentService {
    @Autowired
    private IndicatorBatchMapper indicatorBatchMapper;
    @Override
    public Pagination<FcapNoContent> selectByContent(String content, Integer pageNo, Integer pageSize) {
        List<FcapNoContent> fcapNoContentList = new ArrayList<>();
        String b = "";
        if(StringUtils.isNotBlank(content)){
            // 数字反转 开始
            content = content.replace(".", "#");
            String[] arr = content.split("#");
            StringBuffer aa = new StringBuffer("");

            for(int i = arr.length -1 ; i>= 0 ; i--){
                aa.append(arr[i]);
                if(i !=0){
                    aa.append(".");
                }

            }
            b = aa.toString();
            content = content.replace("#", ".");

            log.info("content:"+ content);
            log.info("reverse:"+ b);
            // 数字反转 结束

            if(arr.length == 2) {
                Map<String, Object> map = new HashMap<>();
                map.put("hasCondition", true);
                map.put("content", content);
                map.put("reverse", b);
                map.put("pageSize", pageSize);
                map.put("pageNo", pageNo);
                set2ParamMap(map, arr);
                log.info("2 query map:{}", JSON.toJSONString(map));

                int count =  indicatorBatchMapper.selectCountContentByMap2(map);
                Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
                if(count <= 0){
                    pagination.setList(fcapNoContentList);
                }
                pagination.setList(indicatorBatchMapper.selectContentByMap2(map));
                return pagination;
            }

            // 从后往前，后1 前2    25.14.38.49.22   -> 49.22.25  22.25.14
            String  condition1 = "";
            String  condition2 = "";
            String  condition3 = "";
            String  condition4 = "";

            // 从中间到头再到尾，前1后2     25.14.38.49.22   -> 25.22.49  14.25.22
            String  condition5 = "";
            String  condition6 = "";

            String  condition7 = "";
            String  condition8 = "";
            if(arr.length == 3) {
                condition1 = arr[0] + "." + arr[1];  // 以这个字符串结尾
                condition2 = arr[2];             // 以这个字符串开头

                condition3 = arr[0];              // 以这个字符串结尾
                condition4 = arr[1] + "." + arr[2];  // 以这个字符串开头

                condition5 = arr[0];            //  以这个字符串开头
                condition6 = arr[2] + "." + arr[1];  // 以这个字符串结尾

                condition7 = arr[1] + "." + arr[0];  //以这个字符串开头
                condition8 = arr[2];                 // 以这个字符串结尾
                Map<String, Object> map = new HashMap<>();
                map.put("hasCondition", true);
                map.put("content", content);
                map.put("reverse", b);
                map.put("condition1", condition1);
                map.put("condition2", condition2);
                map.put("condition3", condition3);
                map.put("condition4", condition4);
                map.put("condition5", condition5);
                map.put("condition6", condition6);
                map.put("condition7", condition7);
                map.put("condition8", condition8);
                map.put("pageSize", pageSize);
                map.put("pageNo", pageNo);
                log.info("query map:{}", JSON.toJSONString(map));

                int count =  indicatorBatchMapper.selectCountContentByMap3(map);
                Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
                if(count <= 0){
                    pagination.setList(fcapNoContentList);
                }
                pagination.setList(indicatorBatchMapper.selectContentByMap3(map));
                return pagination;
            }

            if(arr.length == 4) {
                condition1 = arr[0] + "." + arr[1] + "." + arr[2];  // 以这个字符串结尾
                condition2 = arr[3];                               // 以这个字符串开头

                condition3 = arr[0] + "." + arr[1];              // 以这个字符串结尾
                condition4 = arr[2] + "." + arr[3];             // 以这个字符串开头

                condition5 = arr[0] ;                           // 以这个字符串结尾
                condition6 = arr[1] + "."  + arr[2] + "." + arr[3];    // 以这个字符串开头

                condition7 = arr[2] + "." + arr[1] + "." + arr[0]; // 以这个字符串开头
                condition8 = arr[3];                               // 以这个字符串结尾

                String  condition9 = arr[1] + "." + arr[0];       // 以这个字符串开头
                String  condition10 = arr[3]  + "." + arr[2];     // 以这个字符串结尾

                String  condition11 = arr[0];                                     // 以这个字符串开头
                String  condition12 = arr[3]  + "." + arr[2] + "." +  arr[1];     // 以这个字符串结尾

                Map<String, Object> map = new HashMap<>();
                map.put("hasCondition", true);
                map.put("content", content);
                map.put("reverse", b);
                map.put("condition1", condition1);
                map.put("condition2", condition2);
                map.put("condition3", condition3);
                map.put("condition4", condition4);
                map.put("condition5", condition5);
                map.put("condition6", condition6);
                map.put("condition7", condition7);
                map.put("condition8", condition8);
                map.put("condition9", condition9);
                map.put("condition10", condition10);
                map.put("condition11", condition11);
                map.put("condition12", condition12);
                map.put("pageSize", pageSize);
                map.put("pageNo", pageNo);
                log.info("query map:{}", JSON.toJSONString(map));

                int count =  indicatorBatchMapper.selectCountContentByMap4(map);
                Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
                if(count <= 0){
                    pagination.setList(fcapNoContentList);
                }
                pagination.setList(indicatorBatchMapper.selectContentByMap4(map));
                return pagination;
            }

            if(arr.length == 5) {
                Map<String, Object> map = new HashMap<>();
                map.put("hasCondition", true);
                map.put("content", content);
                map.put("reverse", b);
                map.put("pageSize", pageSize);
                map.put("pageNo", pageNo);
                set5ParamMap(map, arr);
                log.info("5 query map:{}", JSON.toJSONString(map));

                int count =  indicatorBatchMapper.selectCountContentByMap5(map);
                Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
                if(count <= 0){
                    pagination.setList(fcapNoContentList);
                }
                pagination.setList(indicatorBatchMapper.selectContentByMap5(map));
                return pagination;
            }

            if(arr.length == 6) {
                Map<String, Object> map = new HashMap<>();
                map.put("hasCondition", true);
                map.put("content", content);
                map.put("reverse", b);
                map.put("pageSize", pageSize);
                map.put("pageNo", pageNo);
                set6ParamMap(map, arr);
                log.info("6 query map:{}", JSON.toJSONString(map));

                int count =  indicatorBatchMapper.selectCountContentByMap6(map);
                Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
                if(count <= 0){
                    pagination.setList(fcapNoContentList);
                }
                pagination.setList(indicatorBatchMapper.selectContentByMap6(map));
                return pagination;
            }
            if(arr.length == 7) {
                Map<String, Object> map = new HashMap<>();
                map.put("hasCondition", true);
                map.put("content", content);
                map.put("reverse", b);
                map.put("pageSize", pageSize);
                map.put("pageNo", pageNo);
                set7ParamMap(map, arr);
                log.info("7 query map:{}", JSON.toJSONString(map));

                int count =  indicatorBatchMapper.selectCountContentByMap7(map);
                Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
                if(count <= 0){
                    pagination.setList(fcapNoContentList);
                }
                pagination.setList(indicatorBatchMapper.selectContentByMap7(map));
                return pagination;
            }

            // 输入7个数字，则不支持从尾到头查询
            Map<String, Object> map = new HashMap<>();
            map.put("pageSize", pageSize);
            map.put("pageNo", pageNo);
            map.put("content", content);
            map.put("reverse", b);
            int count =  indicatorBatchMapper.selectCountByContent(map);
            Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
            if(count <= 0){
                pagination.setList(fcapNoContentList);
            }
            pagination.setList(indicatorBatchMapper.selectByContent(map));
            return pagination;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", pageSize);
        map.put("pageNo", pageNo);
        map.put("content", null);
        map.put("reverse", null);
        int count =  indicatorBatchMapper.selectCountByContent(map);
        Pagination<FcapNoContent> pagination = new Pagination<>(pageNo, pageSize, count);
        if(count <= 0){
            pagination.setList(fcapNoContentList);
        }
        pagination.setList(indicatorBatchMapper.selectByContent(map));
        return pagination;
    }

    private void set2ParamMap(Map<String, Object> map, String[] arr){
        String condition1 = arr[0];  // 以这个字符串结尾
        String condition2 = arr[1];  // 以这个字符串开头

        String condition3 = arr[0];  // 以这个字符串开头
        String condition4 = arr[1];  // 以这个字符串结尾

        map.put("condition1", condition1);
        map.put("condition2", condition2);
        map.put("condition3", condition3);
        map.put("condition4", condition4);
    }
    private void set5ParamMap(Map<String, Object> map, String[] arr){
        String condition1 = arr[0] + "." + arr[1] + "." + arr[2] + "." + arr[3];  // 以这个字符串结尾
        String condition2 = arr[4];                               // 以这个字符串开头

        String condition3 = arr[0] + "." + arr[1] + "." + arr[2];  // 以这个字符串结尾
        String condition4 =   arr[3]+ "." + arr[4];                // 以这个字符串开头

        String condition5 = arr[0] + "." + arr[1];                             // 以这个字符串结尾
        String condition6 = arr[2] + "." + arr[3]+ "." + arr[4];                // 以这个字符串开头

        String condition7 = arr[0];                                                 // 以这个字符串结尾
        String condition8 = arr[1] + "." + arr[2] + "." + arr[3]+ "." + arr[4];                // 以这个字符串开头

        String condition9 = arr[3]+ "." + arr[2] + "." + arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition10 = arr[4];                                               //  以这个字符串结尾

        String condition11 =  arr[2] + "." + arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition12 = arr[4] + "." + arr[3];               //  以这个字符串结尾

        String condition13 =  arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition14 = arr[4] + "." + arr[3] + "." + arr[2]; //  以这个字符串结尾

        String condition15 =  arr[0];    // 以这个字符串开头
        String condition16 =  arr[4] + "." + arr[3] + "." + arr[2] + "." + arr[1]; //  以这个字符串结尾

        map.put("condition1", condition1);
        map.put("condition2", condition2);
        map.put("condition3", condition3);
        map.put("condition4", condition4);
        map.put("condition5", condition5);
        map.put("condition6", condition6);
        map.put("condition7", condition7);
        map.put("condition8", condition8);
        map.put("condition9", condition9);
        map.put("condition10", condition10);
        map.put("condition11", condition11);
        map.put("condition12", condition12);
        map.put("condition13", condition13);
        map.put("condition14", condition14);
        map.put("condition15", condition15);
        map.put("condition16", condition16);
    }

    private void set6ParamMap(Map<String, Object> map, String[] arr){
        String condition1 = arr[0] + "." + arr[1] + "." + arr[2] + "." + arr[3] + "." + arr[4];  // 以这个字符串结尾
        String condition2 = arr[5];                               // 以这个字符串开头

        String condition3 = arr[0] + "." + arr[1] + "." + arr[2] + "." + arr[3];  // 以这个字符串结尾
        String condition4 = arr[4]+ "." + arr[5];                // 以这个字符串开头

        String condition5 = arr[0] + "." + arr[1] + "." + arr[2];                             // 以这个字符串结尾
        String condition6 = arr[3] + "." + arr[4]+ "." + arr[5];                // 以这个字符串开头

        String condition7 = arr[0] + "." + arr[1];                                                 // 以这个字符串结尾
        String condition8 = arr[2] + "." + arr[3] + "." + arr[4]+ "." + arr[5];                // 以这个字符串开头

        String condition9 = arr[0];    // 以这个字符串结尾
        String condition10 = arr[1] + "." + arr[2] + "." + arr[3] + "." + arr[4]+ "." + arr[5]; //  以这个字符串开头                                             //  以这个字符串结尾

        String condition11 = arr[4] + "." + arr[3]+ "." + arr[2] + "." + arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition12 = arr[5];               //  以这个字符串结尾

        String condition13 = arr[3]+ "." + arr[2] + "." + arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition14 = arr[5] + "." + arr[4];               //  以这个字符串结尾

        String condition15 = arr[2] + "." + arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition16 = arr[5] + "." + arr[4]+ "." + arr[3];               //  以这个字符串结尾

        String condition17 = arr[1] + "." + arr[0];                     // 以这个字符串开头
        String condition18 = arr[5] + "." + arr[4]+ "." + arr[3] + "." + arr[2];             //  以这个字符串结尾

        String condition19 = arr[0];                     // 以这个字符串开头
        String condition20 = arr[5] + "." + arr[4]+ "." + arr[3] + "." + arr[2] + "." + arr[1];   //  以这个字符串结尾

        map.put("condition1", condition1);
        map.put("condition2", condition2);
        map.put("condition3", condition3);
        map.put("condition4", condition4);
        map.put("condition5", condition5);
        map.put("condition6", condition6);
        map.put("condition7", condition7);
        map.put("condition8", condition8);
        map.put("condition9", condition9);
        map.put("condition10", condition10);
        map.put("condition11", condition11);
        map.put("condition12", condition12);
        map.put("condition13", condition13);
        map.put("condition14", condition14);
        map.put("condition15", condition15);
        map.put("condition16", condition16);
        map.put("condition17", condition17);
        map.put("condition18", condition18);
        map.put("condition19", condition19);
        map.put("condition20", condition20);
    }

    private void set7ParamMap(Map<String, Object> map, String[] arr){
        String condition1 = arr[0] + "." + arr[1] + "." + arr[2] + "." + arr[3] + "." + arr[4] + "." + arr[5];  // 以这个字符串结尾
        String condition2 = arr[6];                               // 以这个字符串开头

        String condition3 = arr[0] + "." + arr[1] + "." + arr[2] + "." + arr[3] + "." + arr[4];  // 以这个字符串结尾
        String condition4 = arr[5]+ "." + arr[6];                // 以这个字符串开头

        String condition5 = arr[0] + "." + arr[1] + "." + arr[2] + "." + arr[3];                             // 以这个字符串结尾
        String condition6 = arr[4] + "." + arr[5]+ "." + arr[6];                // 以这个字符串开头

        String condition7 = arr[0] + "." + arr[1] + "." + arr[2];                  // 以这个字符串结尾
        String condition8 = arr[3] + "." + arr[4] + "." + arr[5]+ "." + arr[6];                // 以这个字符串开头

        String condition9 = arr[0] + "." + arr[1];    // 以这个字符串结尾
        String condition10 = arr[2] + "." + arr[3] + "." + arr[4] + "." + arr[5]+ "." + arr[6]; //  以这个字符串开头                                             //  以这个字符串结尾

        String condition11 = arr[0];                    // 以这个字符串开头
        String condition12 = arr[1] + "." + arr[2] + "." + arr[3] + "." + arr[4] + "." + arr[5]+ "." + arr[6];  //  以这个字符串结尾

        String condition13 = arr[5] + "." + arr[4] + "." + arr[3]+ "." + arr[2] + "." + arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition14 = arr[6];               //  以这个字符串结尾

        String condition15 = arr[4] + "." + arr[3]+ "." + arr[2] + "." + arr[1] + "." + arr[0];    // 以这个字符串开头
        String condition16 = arr[6] + "." + arr[5];               //  以这个字符串结尾

        String condition17 = arr[3]+ "." + arr[2] + "." + arr[1] + "." + arr[0];                     // 以这个字符串开头
        String condition18 = arr[6]+ "." + arr[5] + "." + arr[4];             //  以这个字符串结尾

        String condition19 = arr[2] + "." + arr[1] + "." + arr[0];                     // 以这个字符串开头
        String condition20 = arr[6] + "." + arr[5]+ "." + arr[4] + "." + arr[3];   //  以这个字符串结尾

        String condition21 = arr[1] + "." + arr[0];                     // 以这个字符串开头
        String condition22 = arr[6] + "." + arr[5]+ "." + arr[4] + "." + arr[3] + "." + arr[2];   //  以这个字符串结尾

        String condition23 = arr[0];                     // 以这个字符串开头
        String condition24 = arr[6] + "." + arr[5]+ "." + arr[4] + "." + arr[3] + "." + arr[2] + "." + arr[1];   //  以这个字符串结尾

        map.put("condition1", condition1);
        map.put("condition2", condition2);
        map.put("condition3", condition3);
        map.put("condition4", condition4);
        map.put("condition5", condition5);
        map.put("condition6", condition6);
        map.put("condition7", condition7);
        map.put("condition8", condition8);
        map.put("condition9", condition9);
        map.put("condition10", condition10);
        map.put("condition11", condition11);
        map.put("condition12", condition12);
        map.put("condition13", condition13);
        map.put("condition14", condition14);
        map.put("condition15", condition15);
        map.put("condition16", condition16);
        map.put("condition17", condition17);
        map.put("condition18", condition18);
        map.put("condition19", condition19);
        map.put("condition20", condition20);
        map.put("condition21", condition21);
        map.put("condition22", condition22);
        map.put("condition23", condition23);
        map.put("condition24", condition24);
    }

    @Override
    public void insertOrUpdateFcapNoContent(FcapNoContent fcapNoContent) {
        if(fcapNoContent.getId() == null){
            indicatorBatchMapper.insertFcapNoContent(fcapNoContent);
        }else{
            indicatorBatchMapper.updateFcapNoContent(fcapNoContent);
        }
    }

    @Override
    public void deleteFcapNoContent(Long id) {
        indicatorBatchMapper.deleteFcapNoContent(id);
    }

    @Override
    @Transactional
    public void batchInsertFcapNoContent(List<FcapNoContent> list) {
        indicatorBatchMapper.batchInsertFcapNoContent(list);
    }

    @Override
    public Pagination<FcapNoContent> selectByMultiContentPage(BatchContentReq req) {
        int count =  indicatorBatchMapper.selectByMultiContentPageCount(req);

        Pagination<FcapNoContent> pagination = new Pagination<>(req.getPageNo(), req.getPageSize(), count);
        if(count <= 0){
            pagination.setList(new ArrayList<FcapNoContent>());
        }
        List<FcapNoContent> list = indicatorBatchMapper.selectByMultiContentPage(req);
        pagination.setList(list);
        return pagination;
    }

    @Override
    public Pagination<FcapNoContent> selectByMultiContentHanziPage(BatchContentReq req) {
        List<KeywordReq> keywordlist = req.getList();
        String joinStr = "";
        List<String> keyList = new ArrayList<>();
        for(KeywordReq k: keywordlist){
            Set<String> set = k.getSet();
            Iterator<String> iterator = set.iterator();
            while(iterator.hasNext()){
                String next = iterator.next();
                keyList.add(next);
            }
        }
        req.setJoinStr(String.join(";", keyList));
        log.info("selectByMultiContentHanziPage joinStr:{}", req.getJoinStr());
        int count =  indicatorBatchMapper.selectByMultiContentPageHanziCount(req);

        Pagination<FcapNoContent> pagination = new Pagination<>(req.getPageNo(), req.getPageSize(), count);
        if(count <= 0){
            pagination.setList(new ArrayList<FcapNoContent>());
        }
        List<FcapNoContent> list = indicatorBatchMapper.selectByMultiContentPageHanzi(req);
        pagination.setList(list);
        return pagination;
    }
}
